package com.kgame5.xtank3d.resource {
	import flash.events.EventDispatcher;
import kgame5.enginex.Res3DPathVO;
import kgame5.k3d.core.obj3d.node3d.Node3D;
import kgame5.k3d.util3d.Factory3D;
import kgame5.k3d.util3d.reslib.AssetLoaderX;

import com.kgame5.xtank3d.helper.LevelXMLReader;
import com.kgame5.xtank3d.helper.MapXMLReader;
import com.kgame5.xtank3d.helper.MetaConfigXMLReader;
import com.kgame5.xtank3d.vo.BlockVO;
import com.kgame5.xtank3d.vo.GroundVO;
import com.kgame5.xtank3d.vo.MapVO;
import com.kgame5.xtank3d.vo.MetaConfigVO;
import com.kgame5.xtank3d.vo.ShotVO;
import com.kgame5.xtank3d.vo.level.LevelVO;
import com.kgame5.xtank3d.vo.tank.BodyMetaVO;

import flash.display.DisplayObject;
import flash.events.Event;

/**
 * @author kk
 * 资源被加载到这里统一管理
 * private function gotoState_Loading():void{
		stateID=1;
		ResLib.getInstance().startLoadMapData(22);
	}
	private function updateState_Loading():void{
		trace(ResLib.getInstance().getLoadingTip(),ResLib.getInstance().getProgress100());
		if(ResLib.getInstance().isOK()){
			trace("完毕");
			this.gotoState_Play();
		}
	}
 */
public class ResourceManagerX extends EventDispatcher{
	
	private static var instance:ResourceManagerX;
	public static function getInstance():ResourceManagerX{
		if(instance==null){
			instance=new ResourceManagerX();
		}
		return instance;
	}
	private var metaConfigVO:MetaConfigVO;
	private var levelVO:LevelVO;
	private var mapVO:MapVO;
	private var al_mapXML:AssetLoaderX;
	private var al_3d:AssetLoaderX;
	
	
	private var _isOK:Boolean=false;
	
	private var cur_al:AssetLoaderX;
	
	public function ResourceManagerX(){
		
	}
	
	//重新加载的时候使用
	public function reset():void{
		_isOK=false;
	}
	public function isOK():Boolean{
		return _isOK;
	}
	public function getLevelVO():LevelVO{
		return levelVO;
	}
	public function getMapVO():MapVO{
		return mapVO;
	}
	public function getMetaConfigVO():MetaConfigVO{
		return metaConfigVO;
	}
	
	public function getProgress100():int{
		if(cur_al==null){
			return 0; 
		}else{
			return cur_al.getProgress100();
		}
	}
	
	public function getLoadingTip():String{
		if(cur_al==null){
			return ""; 
		}else{
			return cur_al.getTip();
		}
	}
	
	public function start(levelID:int):void{
		this.startLoadLevelData(levelID);
	}
	
	private function onAllOK():void{
		//发送事件说完毕了
		this.dispatchEvent(new Event(Event.COMPLETE));
	}
	//---------------------加载关卡数据--------------------
	private var al_level:AssetLoaderX;
	private var levelID:int;//为了提取加载结果用
	private function startLoadLevelData(levelID:int):void{
		this.levelID=levelID;
		al_level=new AssetLoaderX("加载关卡数据...");
		al_level.addEventListener(AssetLoaderX.EVENT_ALL_OK, onLoadLevelDataOK);
		al_level.addUrl(PathManager.metaconfig());
		al_level.addUrl(PathManager.level(levelID));
		al_level.load();
		
		cur_al=al_level;
	}
	
	private function onLoadLevelDataOK(e:Event):void{
		//trace("onLoadLevelDataOK");
		var s1:String=al_level.getData(PathManager.metaconfig()) as String;
		var s:String=al_level.getData(PathManager.level(levelID)) as String;
		
		metaConfigVO=(new MetaConfigXMLReader()).read(new XML(s1));
		levelVO=(new LevelXMLReader()).read(new XML(s));
		
		levelVO._metaConfigVO=metaConfigVO;
		
		//-加载地图
		startLoadMapData(levelVO.mapID);
	}
	
	//---------------------加载地图数据--------------------
	private var mapID:int;
	public function startLoadMapData(mapID:int):void{
		this.mapID=mapID;
		al_mapXML=new AssetLoaderX("加载地图数据...");
		al_mapXML.addEventListener(AssetLoaderX.EVENT_ALL_OK, onLoadMapDataOK);
		al_mapXML.addUrl(PathManager.mapdata(mapID));
		al_mapXML.load();
		
		cur_al=al_mapXML;
	}
	
	private function onLoadMapDataOK(e:Event):void{
		//trace("onLoadMapDataOK");
		var s:String=al_mapXML.getData(PathManager.mapdata(mapID)) as String;
		
		mapVO=MapXMLReader.CreateFromXML(new XML(s));
		
		//!!这个很重要，这个是需要出生新坦克用的
		levelVO._mapVO=mapVO;
		
		//-加载3d资源
		startLoad3DAssets();
	}
	
	//----------------加载3D资源-------------------
	private function startLoad3DAssets():void{
		//trace("startLoad3DAssets");
		al_3d=new AssetLoaderX("加载3D资源...");
		al_3d.addEventListener(AssetLoaderX.EVENT_ALL_OK, onLoad3DOK);
		
		//地面
		add3DFile(PathManager.ground(mapVO.groundVO.typeID));
		
		//障碍物
		for each(var barVO:BlockVO in mapVO.dict_barVO){
			//add3DFile(barVO._pathVO);
			add3DFile(PathManager.block(barVO.typeID));
		}
		
		//坦克,TODO 这个是可以写到level.xml文件中的
		add3DFile(PathManager.tankBarb(1));
		add3DFile(PathManager.tankBody(1));
		add3DFile(PathManager.tankWheel(1,"A"));
		add3DFile(PathManager.tankWheel(1,"B"));
		add3DFile(PathManager.tankWheel(1,"C"));
		
		//自爆卡车
		for each(var bornVO:BodyMetaVO in metaConfigVO.dict_bodyDef){
			var bodyMetaID:int= bornVO.metaID;
			add3DFile(PathManager.tankBody(bodyMetaID));
			add3DFile(PathManager.tankWheel(bodyMetaID,"A"));
			add3DFile(PathManager.tankWheel(bodyMetaID,"C"));
		}
		
		//坦克碎片
		add3DFile(PathManager.tankChip());
		
		//其他碎片
		add3DFile(PathManager.effChip(3));
		add3DFile(PathManager.effChip(4));
		add3DFile(PathManager.effChip(5));
		
		add3DFile(PathManager.shotShell(1));
		//启动
		al_3d.load();
		
		cur_al=al_3d;
	}
	
	private function onLoad3DOK(e:Event):void{
		//trace("onLoad3DOK");
		_isOK=true;
		
		onAllOK();
	}
	
	//加载3D文件
	private function add3DFile(pathVO:Res3DPathVO):void{
		al_3d.addUrl(pathVO.md2Path);
		al_3d.addUrl(pathVO.skinPath);
		if(pathVO.mtabPath!=null){
			al_3d.addUrl(pathVO.mtabPath);
		}
	}
	
	//-----------------------弹药-----------------------
	public function getCannonShot(shotVO:ShotVO):Node3D{
		return Factory3D.CreateSprite3D("MyMc_NShot"+shotVO.typeID);
	}
	
	//--影子
	public function getShadow(R:int):Node3D{
		var md:Node3D=Factory3D.CreateShadow(R*2,"MyBmd_Shadow");
		//md.layerID = FlagGame.LAYER_SHADOW;
		return md;
	}
	
	//--弹壳
	public function getShotShell(shotVO:ShotVO):Node3D{
		var rn:Res3DPathVO =PathManager.shotShell();//shotVO.typeID
		var md:Node3D=al_3d.getAnimator3D(rn.md2Path, rn.mtabPath, rn.skinPath);
		return md;
	}
	
//	
//	public function getPoisonShot(shotVO:ShotVO):Node3D{
//		var rn:Res3DName= Res3DNameManager.shot(shotVO.typeInfo.type);
//		return getAnimator3D(rn.meshUrl,rn.mtabUrl, rn.skinUrl);
//	}
//	
//	public function getMissileShot(shotVO:ShotVO):Node3D{
//		var rn:Res3DName=Res3DNameManager.shot(shotVO.typeInfo.type);
//		return getAnimator3D(rn.meshUrl,rn.mtabUrl, rn.skinUrl);
//	}
//	
//	public function getHammerShot(shotVO:ShotVO):Node3D{
//		var rn:Res3DName=Res3DNameManager.shot(shotVO.typeInfo.type);
//		return getAnimator3D(rn.meshUrl,rn.mtabUrl, rn.skinUrl);
//	}
//	
//	//这个是粒子效果用的
//	public function getHammerShotX(shotType:int):Node3D{
//		var rn:Res3DName=Res3DNameManager.shot(shotType);
//		return getAnimator3D(rn.meshUrl,rn.mtabUrl, rn.skinUrl);
//	}
//	
//	//-----------------------花儿-----------------------
//	public function getFlower(vo:FlowerVO):Node3D{
//		
//		//道具
//		var mc:MovieClip=Util.AttachMC("MyMc_Flower");
//		//--mc要加载图片
//		var url:String=PicPathManager.getGItemPic(vo.typeInfo.classic, vo.typeInfo.type);
//		mc.mc_picC.addChild(new ASWingFreePic(mc.width,mc.height,url));
//		
//		var md_item:Node3D=new Sprite3D(mc);
//
//		return md_item;
//		
//		
//	}
	
	//---------------------地面-------------------
	public function getGround(vo:GroundVO):Node3D{
		var rn:Res3DPathVO =PathManager.ground(vo.typeID);
		var md:Node3D=al_3d.getAnimator3D(rn.md2Path, rn.mtabPath, rn.skinPath);

		md.rotY=180;//注意为了和3dmax中一致，需要设置为180
		
		return md;
	}
	
	//---------------------障碍物-----------------
	public function getBlock(vo:BlockVO):Node3D{
		var rn:Res3DPathVO=PathManager.block(vo.typeID);
		var md:Node3D=al_3d.getAnimator3D(rn.md2Path, rn.mtabPath, rn.skinPath);
		
		return md;
	}
	
	//-------------------坦克-----------------
	public function getTankBarb(typeID:int):Node3D{
		var rn:Res3DPathVO=PathManager.tankBarb(typeID);
		var md:Node3D=al_3d.getAnimator3D(rn.md2Path, rn.mtabPath, rn.skinPath);
		return md;
	}
	
	public function getTankBody(typeID:int):Node3D{
		var rn:Res3DPathVO=PathManager.tankBody(typeID);
		var md:Node3D=al_3d.getAnimator3D(rn.md2Path, rn.mtabPath, rn.skinPath);
		return md;
	}
	
	public function getTankWheel(typeID:int,mark:String):Node3D{
		var rn:Res3DPathVO=PathManager.tankWheel(typeID,mark);
		var md:Node3D=al_3d.getAnimator3D(rn.md2Path, rn.mtabPath, rn.skinPath);
		return md;
	}
	
//	public function getMouse3D():Node3D{
//		var md:Node3D=Factory3D.CreateShadow(64, "MyBmd_Mouse3D");
//		md.layerID = FlagGame.LAYER_AIMMARK;//光标层
//		return md;
//	}
//	
//	//------------------导弹标志----------------
//	public function getAimMark(vo:AimMarkVO):Node3D{
//		
//		var md:Node3D;
//		switch(vo.type){
//			case AimMarkVO.SPRITE3D:
//			md=Factory3D.CreateSprite3D(vo.sLinkID);
//			md.layerID = FlagGame.LAYER_TIP;
//			break;
//			case AimMarkVO.SHADOW:
//			md=Factory3D.CreateShadow(64, vo.sLinkID);
//			md.layerID = FlagGame.LAYER_AIMMARK;
//			break;
//		}
//		
//		return md;
//	
//	}
//	
//	//-------------------粒子效果------------------
	public function getBlastChip():Node3D{
		return Factory3D.CreateSprite3D("MyMc_BlastChip");
	}
	public function getBlastWave(r:int):Node3D{
		return Factory3D.CreateShadow(r*2,"MyBmd_blastWave");
	}
	public function getBlastMain():Node3D{
		return Factory3D.CreateSprite3D("MyMc_Blast");
	}
	//垂直爆炸
	public function getBlastVertical():Node3D{
		return Factory3D.CreateSprite3D("MyMc_BlastVertical");
	}

	public function getCannonSmoke():Node3D{
		return Factory3D.CreateSprite3D("MyMc_CannonSmoke");
	}
	public function getCraterShadow(r:int):Node3D{
		return Factory3D.CreateShadow(r*2,"MyBmd_Crater");
	}
	public function getDieBloodChip(r:int):Node3D{
		return Factory3D.CreateShadow(r*2,"MyMc_Blood");
	}
	public function getDust():Node3D{
		return Factory3D.CreateSprite3D("MyMc_Dust");
	}
	public function getHurtBloodChip(r:int):Node3D{
		return Factory3D.CreateShadow(r*2,"MyMc_Blood");
	}
	public function getPoisonGround(r:int):Node3D{
		return Factory3D.CreateShadow(r*2,"MyBmd_poisonGround");
	}
	public function getPoisonPso():Node3D{
		return Factory3D.CreateSprite3D("MyMc_poisonPao");
	}
	public function getQuakeWaveGround(r:int):Node3D{
		return Factory3D.CreateShadow(r*2,"MyBmd_hammerGround");
	}
	public function getSpark():Node3D{
		return Factory3D.CreateSprite3D("MyMc_PropSpark");
	}
	public function getSuicideDust():Node3D{
		return Factory3D.CreateSprite3D("MyMc_BloodDust");
	}
	public function getTankDieWave():Node3D{
		return Factory3D.CreateSprite3D("MyMc_TankDieWave");
	}
	public function getCollPower():Node3D{
		return Factory3D.CreateSprite3D("MyMc_CollPower",true,false);
	}
	
	public function getFlySmoke(type:int):Node3D{
		return Factory3D.CreateSprite3D("MyMc_FlySmoke_"+type);
	}
	
	public function getGold():Node3D{
		return Factory3D.CreateSprite3D("MyMc_Gold");
	}
	
	
	public function getBlockChip(chipTypeID:int):Node3D{
		var rn:Res3DPathVO =PathManager.effChip(chipTypeID);//
		var md:Node3D=al_3d.getAnimator3D(rn.md2Path, rn.mtabPath, rn.skinPath);
		return md;
	}
	public function getTankHurtChip():Node3D{
		var rn:Res3DPathVO =PathManager.effChip(3);
		var md:Node3D=al_3d.getAnimator3D(rn.md2Path, rn.mtabPath, rn.skinPath);
		return md;
	}
	public function getTankDieChip():Node3D{
		var rn:Res3DPathVO =PathManager.tankChip();
		var md:Node3D=al_3d.getAnimator3D(rn.md2Path, rn.mtabPath, rn.skinPath);
		return md;
	}
	//GameLib3D.getInstance().
	
	//描边用的
	private function blackEdge(mc:DisplayObject):void{
		//mc.cacheAsBitmap=true;
		//mc.opaqueBackground=true;
		
//		//是否描边
//		if(SettingVO.blackEdge) {
//			mc.filters=[new GlowFilter(0x000000,1,2,2,1000,1)];
//		}
	}
}//end of class
}
